import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def tset_shop_list(self):
        # 商品列表查询测试
        
        # 点击商品
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        self.browser.find_element(By.XPATH, '//*[@id="keyword"]').send_keys('文具')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        
        # 接收列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        # 接收条数
        tr_count = len(tr_list)
        if tr_count == 0:
            print('没有数据')
        is_not_bug = True
        for i in tr_list:
            shop_name = i.find_element(By.XPATH, './td[2]/div').text
            if '文具' not in shop_name:
                is_not_bug = False
        if is_not_bug:
            print('商品列表查询没有问题')
        else:
            print('商品列表有问题')
        sleep(2)
        
    def test_login(self):
        # 登录测试
        
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        
        # 输入用户名
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        
        # 输入密码
        self.browser.find_element(By.NAME, 'password').send_keys('admin')
        
        # 输入验证码
        self.browser.find_element(By.NAME, 'code').send_keys('111111')
        
        # 点击登录
        self.browser.find_element(By.ID, 'login').click()
        sleep(3)
        
    def test_shop_add(self):
        # 商品新增方法
        
        # 点击商品
        self.browser.switch_to.default_content()
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位商品子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        # 点击发布商品按钮
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        
        # 定位商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe) 
        
        # 获取当前时间
        now = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        # 定位商品名称并填写信息
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品' + now)
        
        # 点击第一个大类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(0.5)
        # 点击第一个大类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(0.5)
        # 点击第二个类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(0.5)
        # 点击第二个类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(0.5)
        # 点击第三个类的下拉框
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(0.5)
        # 点击第三个类的下拉数据的第一个数据
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        
        # 商品主图
        # 定位商品主图按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        
        # 进入到上传图片子框架
        # 定位上传图片子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        self.test_upload_img('D:\\poen\\Software_Test\\selenium_work\\截图.PNG')
        
        # 商品轮播图
        # 回到商品发布子框架 先回到主框架，再进入商品子框架、最后进入商品发布子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 定位商品轮播图 图片上传
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        # 进入图片上传子框架
        upload_img_iframe_2 = self.browser.find_element(By.XPATH, '/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('D:\\poen\\Software_Test\\selenium_work\\截图.PNG')
        
        # 回到商品发布子框架 先回到主框架，再进入商品子框架、最后进入商品发布子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 点击价格库存
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(0.5)
        
        # 定位价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        sleep(1)
        
        # 点击保存按钮，因为保存是在商品子框架，所以需要回到商品子框架
        self.browser.switch_to.default_content()
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        
    def test_upload_img(self, file_path):
        # 图片上传
        
        # 通过文件输入框完成图片上传操作
        self.browser.find_element(By.XPATH, '/html/body/div[2]/div[1]/input').send_keys(file_path)
        sleep(5)
        # 选中第一张图片
        self.browser.find_element(By.XPATH, '//*[@id="lists"]/li[1]/div/img').click()
        sleep(1)
        # 点击选中使用图片按钮
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
         
    def test_shop_sj(self):
        # 商品上架
        
        # 点击仓库中商品
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[2]').click()
        
        # 接收列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(tr_list) > 0 :
            # 上架列表中的第一个商品
            self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[2]').click()
            sleep(2)
            
            # 点击确定
            self.browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/a[1]').click()
            
            
    def test_shop_edit(self):
        # 修改商品信息
        
        # 点击商品
        self.browser.switch_to.default_content()
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        # 定位商品子框架
        iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        # 点击销售中商品
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]').click()
        sleep(1)
        
        # 接收列表
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(tr_list) > 0 :
            # 修改列表中的第一个商品
            # 定位修改框架
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
            
            # 定位商品编辑子框架
            shop_edit_iframe = self.browser.find_element(By.XPATH, '/html/body/div[3]/div[2]/iframe')
            self.browser.switch_to.frame(shop_edit_iframe) 
            
            # 修改商品名称,先清除再输入
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').click()
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
            sleep(1)
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('软件测试')
            sleep(1)
            
            # 定位价格库存
            self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
            
            # 定位价格、成本价、库存输入框并输入数据
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('100')
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').clear()
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('100')
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').clear()
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('100')
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').clear()
            self.browser.find_element(By.XPATH, '//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('999')
            sleep(1)
            
            # 点击保存按钮，因为保存是在商品子框架，所以需要回到商品子框架
            self.browser.switch_to.default_content()
            
            # 定位商品子框架
            iframe1 = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
            self.browser.switch_to.frame(iframe1)
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2)
            
    def test_logout(self):
        # 测试退出
        self.browser.switch_to.default_content()
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        
        # 点击退出
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
         
    
        
if __name__ == '__main__':
    likeShopTest = LikeShopTest()
    likeShopTest.test_login()
    # likeShopTest.tset_shop_list()
    likeShopTest.test_shop_add()
    likeShopTest.test_shop_sj()
    likeShopTest.test_shop_edit()
    likeShopTest.test_logout()